package com.hundun.hddeal.service.lmpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hundun.hddeal.dao.GzhMapper;
import com.hundun.hddeal.dao.OrderDetailMapper;
import com.hundun.hddeal.dao.OrderMapper;
import com.hundun.hddeal.entity.*;
import com.hundun.hddeal.response.Result;
import com.hundun.hddeal.response.ResultUtil;
import com.hundun.hddeal.service.OrderDetailService;
import com.hundun.hddeal.util.MD5Util;
import com.hundun.hddeal.vo.StatusCountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Date;
import java.util.List;
import static org.assertj.core.util.DateUtil.now;

/**
 * 预约公众号订单业务层
 * @author 谢
 */
@Service
public class OrderDetailServiceImpl implements OrderDetailService {

    @Autowired
    OrderDetailMapper orderDetailMapper;
    @Autowired
    GzhMapper gzhMapper;
    @Autowired
    OrderMapper orderMapper;

    @Override
    public Result<OrderDetail> addOrderDetail(OrderDetail orderDetail) {
        try{
            orderDetailMapper.insertSelective(orderDetail);
            return  ResultUtil.success();
        }catch (Exception e){
            return ResultUtil.error();
        }

    }

    @Override
    public String pay(Long orderId) {
        return null;
    }

    /**
     * 查找Ad用户订单列表
     * @param adId
     * @return
     */
    @Override
    public Result<PageInfo<OrderNew>> getAll(String adId, Byte status, Integer page,
                                             Integer size, String needName, Date startTime, Date endTime,String hostId,String orderId,String gzhName) {
        try{
            PageHelper.startPage(page, size);
            List<OrderNew> orderDetailList = orderDetailMapper.selectByAdIdAndStatus(adId,status,needName,startTime,
                    endTime,hostId,orderId,gzhName);
            for(OrderNew orderNew:orderDetailList){
                Base64.Decoder decoder = Base64.getDecoder();
                orderNew.setTextBody(new String(decoder.decode(orderNew.getTextBody()), StandardCharsets.UTF_8));
                if(orderNew.getStatus()==0 || orderNew.getStatus()==13 ||orderNew.getStatus()==12
                        ||orderNew.getStatus()==7){
                    orderNew.setRealityMoney(0.00);
                }else {
                    BigDecimal bg = new BigDecimal(orderNew.getOrderMoney()-orderNew.getOutMoney());
                    double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    //应约价-质检=消费金额
                    orderNew.setRealityMoney(f1);
                }
            }
            PageInfo<OrderNew> pageInfo = new PageInfo<>(orderDetailList);
            return ResultUtil.success(pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error();
        }

    }
    /**
     * 查找mt用户订单列表
     * @param mtId
     * @return
     */
    @Override
    public  Result<PageInfo<OrderDetail>> getAll1(String mtId, Byte status, Integer page, Integer size, String orderId, String needName,
                                                 String gzhName, Date startTime, Date endTime,String hostId){
        try{
            PageHelper.startPage(page, size);
            List<OrderDetail> orderDetailList = orderDetailMapper.selectByMtIdAndStatus(mtId,status,orderId,needName,gzhName,startTime,endTime,hostId);
            for(OrderDetail orderDetail:orderDetailList){
                Gzh gzh= gzhMapper.selectByGhId(orderDetail.getGhId());
                if(gzh.getGzhPwd()!=null){
                    orderDetail.setGzhPwd(MD5Util.MD(gzh.getGzhPwd()));
                }
                Base64.Decoder decoder = Base64.getDecoder();
                orderDetail.setTextBody(new String(decoder.decode(orderDetail.getTextBody()), StandardCharsets.UTF_8));
            }
            PageInfo<OrderDetail> pageInfo = new PageInfo<>(orderDetailList);
            return ResultUtil.success(pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error();
        }

    }
    @Override
    public Result<OrderNew> selectByPrimaryKey(Long id) {
        try{
            OrderNew orderDetail=orderDetailMapper.selectByPrimaryKey(id);
            Base64.Decoder decoder = Base64.getDecoder();
            orderDetail.setTextBody(new String(decoder.decode(orderDetail.getTextBody()), StandardCharsets.UTF_8));
            return ResultUtil.success(orderDetail);
        }catch (Exception e){
            return  ResultUtil.error();
        }
    }

    @Override
    public Result updateByPrimaryKeySelective(OrderDetail record) {
        try{
            if(record.getTakeUrl()!=null){
                record.setRealityTgTime(now());
            }
            if(record.getStatus()==2){
                List<Gzh> gzhs=gzhMapper.selectByGzhId(record.getIds());
                for(Gzh gzh:gzhs){
                    gzh.setOrderTime(now());
                    gzh.setScoreReceipt(0);
                    //接单扣分
                    gzhMapper.updateByPrimaryKeySelective(gzh);
                }
            }
            orderDetailMapper.updateByPrimaryKeySelective(record);
          return ResultUtil.success();

        }catch (Exception e){
            return  ResultUtil.error();
        }
    }

    @Override
    public Result updateOrederStatus(Byte status, String orderId) {
        try{
            orderDetailMapper.updateOrederStatus(status,orderId);
            return ResultUtil.success();
        }catch (Exception e){
            return  ResultUtil.error();
        }
    }

    @Override
    public Result<PageInfo<Order>> mtHostList(String mtId,Integer page,Integer size,String hostId,
                                                  String needName,Date startTime, Date endTime,Byte status) {
        try{
            PageHelper.startPage(page, size);
            List<Order> mtOrders=orderMapper.getMtOrder(mtId,hostId,needName,startTime,endTime,status);
            for (Order order:mtOrders) {
                List<OrderDetail> orderDetails = orderDetailMapper.getDetails(order.getHostId(), null, mtId);
                order.setGzhNum(orderDetails.size());
            }
            //List<OrderDetail> hostList =orderDetailMapper.mtHostList(mtId,hostId,needName,startTime,endTime);
            PageInfo<Order> pageInfo = new PageInfo<>(mtOrders);
            return ResultUtil.success(pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            return  ResultUtil.error();
        }
    }
    @Override
    public Result<PageInfo<Order>> adHostList(String adId, Integer page, Integer size, String hostId, String needName, Date startTime, Date endTime,Byte status) {
        try{

            PageHelper.startPage(page, size);
            List<Order> adOrders=orderMapper.getAdOrder(adId,hostId,needName,startTime,endTime,status);
            for(Order order:adOrders){
                //计算要减去的审核中或者拒绝取消的钱
                double taxMoney=0.00;
                //9不通过状态查询
                List<OrderNew> orderNews= orderDetailMapper.selectByAdIdAndStatus(adId,(byte)9,needName,startTime,endTime,order.getHostId(),null,null);

                if(orderNews.size()!=0) {

                    for (OrderNew orderNew : orderNews) {
                        if (orderNew.getStatus() == 7 || orderNew.getStatus() == 12 || orderNew.getStatus() == 8) {
                            taxMoney += orderNew.getOrderMoney() - orderNew.getOutMoney();
                        }
                    }
                    BigDecimal bg = new BigDecimal(order.getSetMoneySum() - taxMoney);
                    double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    order.setSetMoneySum(f1);
                    //全部流单
                    if(order.getStatus()==4||order.getStatus()==7||order.getStatus()==8){
                        order.setSetMoneySum(0.00);
                    }

                }

            }
            PageInfo<Order> pageInfo = new PageInfo<>(adOrders);
            return ResultUtil.success(pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            return  ResultUtil.error();
        }
    }

    @Override
    public Result getAdCount(String adId) {
        try{
           StatusCountVo statusCountVo= orderMapper.getAdCount(adId);
           if(statusCountVo==null){
               StatusCountVo statusCountVo1=new StatusCountVo();
               return ResultUtil.success(statusCountVo1);
           }
           return ResultUtil.success(statusCountVo);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error();
        }
    }

    @Override
    public Result getMtCount(String mtId) {
        try{
            StatusCountVo statusCountVo= orderMapper.getMtCount(mtId);
            if(statusCountVo==null){
                StatusCountVo statusCountVo1=new StatusCountVo();
                return ResultUtil.success(statusCountVo1);
            }
            return ResultUtil.success(statusCountVo);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error();
        }
    }
}


